<?php
/**
 * Created by PhpStorm.
 * User: 96572
 * Date: 2017/4/29
 * Time: 16:09
 */

namespace app\modules\admin\controllers;

use Yii;
use app\common\CommonController;
use app\modules\admin\models\Menu;
use yii\helpers\Url;
use yii\web\Response;

class MenuController extends CommonController
{
    public function actionCreate()
    {
        $menu = new Menu();
        $request = Yii::$app->request;
        $post = $request->post($menu->formName());
        if($post['menu_is_lowest'] == 1)
        {
            $menu->scenario = Menu::SCENARIO_IS_LOWEST_INSERT;
        }
        else
        {
            $menu->scenario = Menu::SCENARIO_NOT_LOWEST_INSERT;
        }
        $menu->load($request->post());
        if ($request->isAjax)
        {
            $menu->validate();
            Yii::$app->response->format = Response::FORMAT_JSON;
            return $menu->getErrors();
        }
        elseif($request->isPost)
        {
            if ($menu->save())
            {
                $this->success(
                    '成功',
                    '添加成功',
                    [
                        [
                            'url'=>Url::toRoute('create'),
                            'name'=>'继续添加'
                        ],
                        [
                            'url'=>Url::toRoute('list'),
                            'name'=>'菜单列表'
                        ]
                    ]);
            }
            else
            {
                $this->error(
                    '添加失败',
                    current(current($menu->getErrors())),
                    [
                        [
                            'url'=>Url::toRoute('create'),
                            'name'=>'重现添加'
                        ]
                    ]);
            }
        }
        else{
            $menus = Menu::find()
                ->asArray()
                ->select(['menu_id','menu_label'])
                ->where(['menu_is_lowest'=>0])
                ->all();
            $menu->scenario = Menu::SCENARIO_NOT_LOWEST_INSERT;
            return $this->render(
                'create',
                [
                    'menu'=>$menu,
                    'menus'=>$menus
                ]
            );
        }
    }

    public function actionList()
    {
        $menus = Menu::getMenuTree();

        return $this->render(
            'list',
            [
                'menus'=>$menus
            ]
        );
    }

    public function actionUpdate($menu_id)
    {
        $menu = Menu::findOne(['menu_id'=>(int)$menu_id]);
        // 设置当前场景
        if ($menu->menu_is_lowest == 1)
        {
            $menu->scenario = Menu::SCENARIO_IS_LOWEST_UPDATE;
        }
        else if ($menu->menu_is_lowest == 0)
        {
            $menu->scenario = Menu::SCENARIO_NOT_LOWEST_UPDATE;
        }

        $request = Yii::$app->request;
        $menu->load($request->post());
        // ajax为验证
        if($request->isAjax)
        {
            $menu->validate();
            Yii::$app->response->format = Response::FORMAT_JSON;
            return $menu->getErrors();
        }

        // get 为显示更新的页面
        if ($request->isGet)
        {
            $menus = Menu::find()
                ->asArray()
                ->select(['menu_id','menu_label'])
                ->where(['menu_is_lowest'=>0])
                ->all();
            return $this->render(
                'update',
                [
                    'menu'=>$menu,
                    'menus'=>$menus
                ]
            );
        }

        // post 为显示更新数据的
        if ($request->isPost)
        {
            if ($menu->save())
            {
                $this->success(
                    '成功',
                    '修改成功',
                    [
                        [
                            'url'=>Url::toRoute(['update','menu_id'=>$menu->menu_id]),
                            'name'=>'继续修改'
                        ],
                        [
                            'url'=>Url::toRoute('list'),
                            'name'=>'菜单列表'
                        ]
                    ]);
            }
            else
            {
                $this->error(
                    '添加失败',
                    current(current($menu->getErrors())),
                    [
                        [
                            'url'=>Url::toRoute(['update','menu_id'=>$menu->menu_id]),
                            'name'=>'重新修改'
                        ]
                    ]);
            }
        }
    }

    public function actionJin()
    {
        $hide = [
            [[8,10],[3,30]],
            [[15,17],[3,30]],
        ];
        for ($i = 0;$i < 100;$i++) {
            for ($j = 0;$j < 260;$j++) {
                $flag = false;
                foreach ($hide as $k => $v) {
                    if (is_array($v[0])) {
                        $flag = $i >= $v[0][0] && $i <= $v[0][1];
                    } else {
                        $flag = $v[0] == $i;
                    }
                    if (is_array($v[1])) {
                        $flag = $flag && $j >= $v[1][0] && $j <= $v[1][1];
                    } else {
                        $flag = $flag && $v[1] == $j;
                    }
                    if ($flag) {
                        break;
                    }
                }

                if ($flag) {
                    echo '*';
                } else {
                    echo '&nbsp;';
                }
            }
            echo '<br>';
        }
    }
}